package com.zheng1.envii

import android.app.Application
import android.util.Log
import androidx.compose.runtime.LaunchedEffect
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.zheng1.envii.btutils.BleDeviceStatus
import com.zheng1.envii.btutils.BleHelper
import com.zheng1.envii.btutils.EnvIIBleDevice
import com.zheng1.envii.ui.components.DeviceStatus
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.forEach
import kotlinx.coroutines.launch
import java.util.UUID

class MainViewModel(application: Application) : AndroidViewModel(application) {

    private val TAG = "MainViewModel"
    private val bleHelper = BleHelper(application)
    private val targetEnvDeviceUUidArray = listOf(
        UUID.fromString("0000F100-0000-1000-8000-00805F9B34FB")
    )


    private val _connectedDeviceList = MutableStateFlow<List<EnvIIBleDevice>>(mutableListOf())
    val connectedDeviceList : StateFlow<List<EnvIIBleDevice>> = _connectedDeviceList

    private val _readyForConnectDeviceList = MutableStateFlow<List<EnvIIBleDevice>>(mutableListOf())
    val readyForConnectDeviceList : StateFlow<List<EnvIIBleDevice>> = _readyForConnectDeviceList

    private val _scanStatus = MutableStateFlow(false)
    val scanStatus : StateFlow<Boolean> = _scanStatus

    fun startScan(){
        bleHelper.scanLeDevice(
            uuidFilter = targetEnvDeviceUUidArray ,
            onScanStart = {
                _scanStatus.value = true
                _readyForConnectDeviceList.value = emptyList()
                Log.d(TAG, "startScan: onScanStart")
            },
            onScanStop = {
                _scanStatus.value = false
                Log.d(TAG, "startScan: onScanStop")
            },
            onDevcieFound = { device ->
                Log.d(TAG, "startScan: onDevcieFound")
                _readyForConnectDeviceList.value += device
            }
        )

    }

    fun stopScan(){
        bleHelper.scanLeDevice(
            uuidFilter = null,
            onScanStart = {},
            onScanStop = {
                _scanStatus.value = false
            },
            onDevcieFound = {}
        )
    }

    fun connect( deviceData: EnvIIBleDevice ) {
        deviceData.connect { newState ->
            when (newState) {
                BleDeviceStatus.CONNECTED -> {
                    _readyForConnectDeviceList.value =
                        _readyForConnectDeviceList.value.filter { readyForConnectDeviceList ->
                            readyForConnectDeviceList != deviceData
                        }
                    _connectedDeviceList.value += deviceData
                }

                BleDeviceStatus.CONNECTING -> {
                }

                BleDeviceStatus.DISCONNECTED -> {
                    _readyForConnectDeviceList.value =
                        _readyForConnectDeviceList.value.filter { readyForConnectDeviceList ->
                            readyForConnectDeviceList != deviceData
                        }
                    _connectedDeviceList.value =
                        _connectedDeviceList.value.filter { connectedDeviceList ->
                            connectedDeviceList != deviceData
                        }
                }
            }
        }
    }

    fun findBleDeviceByAddress(address : String) : EnvIIBleDevice? {
        _connectedDeviceList.value.forEach {
            if(it.address == address) return it
        }
        return null
    }

}